//
//  BGGoodInfoFramDataModel.m
//  bigMall
//
//  Created by LTOVE on 16/8/2.
//  Copyright © 2016年 thinklev. All rights reserved.
//

#import "BGGoodInfoFramDataModel.h"
#import "BGProductDetialDataModel.h"
#import "BGProductParamModel.h"
#import "BGMessageContentFrameModel.h"
#import "BGProductIndexDataModel.h"
#import "BGQCreportDataModel.h"

#import "BGGoodInfoResult.h"
#import "BGGoodAttrsModel.h"

#import "BGBasicUrl.h"

#import "BGCheckReportModel.h"

#import "BGViewladderpriceDataModel.h"

#import "BGStatePriceStrings.h"

#import "BGbidRecordListDataModel.h"
#import "BGAuctionLogListModel.h"

#import "BGBidRecodeListDataModel.h"

#import "BGAuctionTopViewDataModel.h"
#import "BGDateFormaTool.h"

#import "BGAuctionInfoModel.h"

@implementation BGGoodInfoFramDataModel

+ (NSArray *)getDataArrayWithBGGoodInfoResult:(BGGoodInfoResult *)result
{
    NSMutableArray *dataArray = [NSMutableArray array];
    BGGoodInfoFramDataModel *bigModel = [[BGGoodInfoFramDataModel alloc]init];
    BGMessageContentFrameModel *Fmodel = [[BGMessageContentFrameModel alloc]init];
    //顶部
    BGProductDetialDataModel *model = [[BGProductDetialDataModel alloc]init];
    model.title = result.title;
    model.supplyQuantity = result.origin_number;
    model.supplyQuantityStr = @"供应量";
    model.price = result.price;
    model.priceStr = @"单价";
    model.browsePopularity = result.view_times;
    model.browsePopularityStr = @"浏览人气";
    model.doubleLineContentListArray = result.detialArray;
    model.endTime = result.end_time;
    Fmodel.dataModel = model;
        //计算高度
//    CGFloat contHeigh = BGTitleHeigh * 2;
//    CGFloat listH = BGTitleHeigh * 3;
//    CGFloat timeHeigh = BGTitleHeigh * 4;
    NSInteger temp = (result.detialArray.count +1 )/ 2;
    
    Fmodel.cellHeigh = BGTitleHeigh + BGTopMidViewHeigh + temp * BGMagListMaginY + temp * BGLabelHeigh + 5 + 2 * BGMaginTop + 2 * BGLabelHeigh + BGMaginButtom ;
    [dataArray addObject:Fmodel];
    //查看阶梯价格
    NSArray *array = [BGStatePriceStrings arrayWithResultArray:result.step_price andUnit:result.unit];
    if (array.count > 0) {
        BGMessageContentFrameModel *visitPriceFmodel = [[BGMessageContentFrameModel alloc]init];
        BGViewladderpriceDataModel *visitPModel = [[BGViewladderpriceDataModel alloc]init];
        //    visitPModel.title = @""
        
        visitPModel.dataArray = array;
        visitPriceFmodel.dataModel = visitPModel;
        visitPriceFmodel.cellHeigh = BGButtonCellHeigh;
        [dataArray addObject:visitPriceFmodel];
    }
    
    
    //产品指标
    BGProductParamModel *Productmodel = [[BGProductParamModel alloc]init];
    Productmodel.title = @"产品指标";
    //循环设置数组
    Productmodel.dataArray = [bigModel getProductInfoArrayWithGoodArray:result.goods_attrs isTender:NO];
    BGMessageContentFrameModel *ProFmodel = [[BGMessageContentFrameModel alloc]init];
    ProFmodel.dataModel = Productmodel;
    //计算行高
    ProFmodel.cellHeigh = Productmodel.dataArray.count ? BGTitleHeigh + ((Productmodel.dataArray.count + 1) /2 ) * BGLabelHeigh + ((Productmodel.dataArray.count + 1) /2 +1) * BGMagListMaginY + BGMaginButtom : BGTitleHeigh;
    [dataArray addObject:ProFmodel];
    //质检报告
    if (result.check_reports.count > 0) {
        BGQCreportDataModel *QcModel = [[BGQCreportDataModel alloc]init];
        QcModel.title = @"质检报告";
        NSMutableArray *QcDataArray = [NSMutableArray new];
        for (BGCheckReportModel *UrlModel in result.check_reports) {
//            NSString *basicUrl = [BGBasicUrl getBasicUrl];
            GWLog(@"%@",UrlModel.url);
            NSString *url = [[BGBasicUrl getBasicUrl] stringByAppendingString:UrlModel.url];//拼接图片URL
            //        NSString *url = @"s";
            GWLog(@"%@",url);
            [QcDataArray addObject:url];
        }
        QcModel.imageDataArray = QcDataArray;
        BGMessageContentFrameModel *QcFmodel = [[BGMessageContentFrameModel alloc]init];
        QcFmodel.dataModel = QcModel;
        //    计算行高
        //    QcFmodel.cellHeigh = QcModel.imageDataArray.count ? BGTitleHeigh + ((QcModel.imageDataArray.count + 1) /2 ) * BGImgListHeigh + ((QcModel.imageDataArray.count + 1) /2 +1) * BGMagListMaginY :BGTitleHeigh;
        QcFmodel.cellHeigh = 143 * (QcDataArray.count / 3 + 1) + 35;
        [dataArray addObject:QcFmodel];
    }
    
    for (BGMessageContentFrameModel *m in dataArray) {
        GWLog(@"%@",[m.dataModel class]);
    }
    
    return dataArray;
}
#pragma mark - 那个啥  那个  下边的列表解析
+ (NSArray *)getDataArrayWithBGAuctionInfoMessageListllllll:(BGAuctionLogListModel *)result
{
    
    NSMutableArray *dataArray = [NSMutableArray array];
    NSArray *dataArrays = result.list;
    
    BGProductIndexDataModel *Productmodel = [[BGProductIndexDataModel alloc]init];
    Productmodel.title = @"出价记录";
    //循环设置数组
    //    Productmodel.dataArray = [bigModel getProductInfoArrayWithGoodArray:result.goods_attrs];
    BGMessageContentFrameModel *ProFmodel = [[BGMessageContentFrameModel alloc]init];
    ProFmodel.dataModel = Productmodel;
    //计算行高
    ProFmodel.cellHeigh = Productmodel.dataArray.count ? BGTitleHeigh + ((Productmodel.dataArray.count + 1) /2 ) * BGLabelHeigh + ((Productmodel.dataArray.count + 1) /2 +1) * BGMagListMaginY + BGMaginButtom : BGTitleHeigh;
    [dataArray addObject:ProFmodel];
    //    []
    
//    BGBidRecodeListDataModelAuCtion
    
    
    
    for (BGAuctionLogListModelList *itemModel in dataArrays) {
        BGBidRecodeListDataModelAuCtion *cellModel = [BGBidRecodeListDataModelAuCtion new];
        BGGoodInfoFramDataModel *bigModel = [[BGGoodInfoFramDataModel alloc]init];
        BGMessageContentFrameModel *Fmodel = [[BGMessageContentFrameModel alloc]init];
        BGbidRecordListDataModel *bidListModel = [BGbidRecordListDataModel new];
        bidListModel.title = itemModel.title;
        bidListModel.datetext = itemModel.add_time;
        
        cellModel.title = [NSString stringWithFormat:@"%@客户",itemModel.phone];
        
        NSString *contextStr = [NSString stringWithFormat:@"出价%@ 元",itemModel.total_amount];
        cellModel.contentText = contextStr;
        cellModel.datetext = [BGDateFormaTool dateFormateWithDateStr:itemModel.add_time dateFormateStr:@"yyyy-MM-dd HH:mm:ss"];
        if ([itemModel.result isEqualToString:@"1"]) {
            cellModel.isBid = YES;
        }else{
            cellModel.isBid = NO;
        }
        cellModel.isColor = YES;
        bidListModel.contentText = contextStr;
        BGMessageContentFrameModel *bidListFmodel = [[BGMessageContentFrameModel alloc]init];
        //    bidListModel.dataModel = bidListModel;
        bidListFmodel.dataModel = cellModel;
        bidListFmodel.cellHeigh = 75;
        [dataArray addObject:bidListFmodel];
    }
    
//    [dataArray writeToFile:@"/Users/LTOVE/Desktop/paimai.plist" atomically:YES];
    return dataArray;
}

+ (NSArray *)getDataArrayWithBGAuctionInfoMessageList:(BGAuctionLogListModel *)result
{
    
    NSMutableArray *dataArray = [NSMutableArray array];
    NSArray *dataArrays = result.list;
    
    BGProductIndexDataModel *Productmodel = [[BGProductIndexDataModel alloc]init];
    Productmodel.title = @"投标记录";
    Productmodel.type = @"tbjl";
    //循环设置数组
    //    Productmodel.dataArray = [bigModel getProductInfoArrayWithGoodArray:result.goods_attrs];
    BGMessageContentFrameModel *ProFmodel = [[BGMessageContentFrameModel alloc]init];
    ProFmodel.dataModel = Productmodel;
    //计算行高
    ProFmodel.cellHeigh = Productmodel.dataArray.count ? BGTitleHeigh + ((Productmodel.dataArray.count + 1) /2 ) * BGLabelHeigh + ((Productmodel.dataArray.count + 1) /2 +1) * BGMagListMaginY + BGMaginButtom : BGTitleHeigh;
    [dataArray addObject:ProFmodel];
    //    []
    
    
    
    
    
    for (BGAuctionLogListModelList *itemModel in dataArrays) {
        BGBidRecodeListDataModel *cellModel = [BGBidRecodeListDataModel new];
        BGGoodInfoFramDataModel *bigModel = [[BGGoodInfoFramDataModel alloc]init];
        BGMessageContentFrameModel *Fmodel = [[BGMessageContentFrameModel alloc]init];
        BGbidRecordListDataModel *bidListModel = [BGbidRecordListDataModel new];
        bidListModel.title = itemModel.title;
        bidListModel.datetext = itemModel.add_time;
        
        cellModel.title = itemModel.phone;
        
        NSString *contextStr = [NSString stringWithFormat:@"以%@元/%@,投标%@ %@%@",itemModel.price,itemModel.unit,itemModel.title,itemModel.number,itemModel.unit];
        cellModel.contentText = contextStr;
        
        cellModel.datetext = [BGDateFormaTool dateFormateWithDateStr:itemModel.add_time dateFormateStr:@"yyyy-MM-dd"];
        cellModel.isColor = NO;
        if ([itemModel.result isEqualToString:@"1"]) {
            cellModel.isBid = YES;
        }else{
            cellModel.isBid = NO;
        }
        bidListModel.contentText = contextStr;
        BGMessageContentFrameModel *bidListFmodel = [[BGMessageContentFrameModel alloc]init];
        //    bidListModel.dataModel = bidListModel;
        bidListFmodel.dataModel = cellModel;
        bidListFmodel.cellHeigh = 75;
        [dataArray addObject:bidListFmodel];
    }
    
//    [dataArray writeToFile:@"/Users/LTOVE/Desktop/paimai.plist" atomically:YES];
    return dataArray;
}

+ (NSArray *)getDataArrayWithBGAuctionInfoResult:(BGAuctionInfoModel *)result
{
    
        BGAuctionInfoModel *models = (BGAuctionInfoModel *)result;
//        models = (BGAuctionInfoModel *)result;
//        result = (BGAuctionInfoModel *)result;
        NSMutableArray *dataArray = [NSMutableArray array];
        BGGoodInfoFramDataModel *bigModel = [[BGGoodInfoFramDataModel alloc]init];
        BGMessageContentFrameModel *Fmodel = [[BGMessageContentFrameModel alloc]init];
        //顶部
        BGProductDetialDataModel *model = [[BGProductDetialDataModel alloc]init];
        model.title = result.title;
        model.secStr = nil;
        model.supplyQuantity = result.origin_number;
        model.supplyQuantityStr = @"供应量";
        model.price = result.price;
        model.priceStr = @"单价";
        model.browsePopularity = result.view_times;
        model.browsePopularityStr = @"浏览人气";
        model.doubleLineContentListArray = result.detialArrayAuction;
        model.endTime = result.end_time;
        Fmodel.dataModel = model;
        //计算高度
        //    CGFloat contHeigh = BGTitleHeigh * 2;
        //    CGFloat listH = BGTitleHeigh * 3;
        //    CGFloat timeHeigh = BGTitleHeigh * 4;
        
        
        Fmodel.cellHeigh = BGTitleHeigh + BGTopMidViewHeigh + 3 * BGMagListMaginY + 3 * BGLabelHeigh + 5 + 2 * BGMaginTop + 2 * BGLabelHeigh + BGMaginButtom ;
        [dataArray addObject:Fmodel];
        //查看阶梯价格
//        BGMessageContentFrameModel *visitPriceFmodel = [[BGMessageContentFrameModel alloc]init];
//        BGViewladderpriceDataModel *visitPModel = [[BGViewladderpriceDataModel alloc]init];
//        //    visitPModel.title = @""
//        
//        visitPModel.dataArray = [BGStatePriceStrings arrayWithResultArray:result.step_price andUnit:result.unit];
//        visitPriceFmodel.dataModel = visitPModel;
//        visitPriceFmodel.cellHeigh = BGButtonCellHeigh;
//        [dataArray addObject:visitPriceFmodel];
    
        //产品指标
        BGProductIndexDataModel *Productmodel = [[BGProductIndexDataModel alloc]init];
        Productmodel.title = @"产品指标";
        //循环设置数组
            Productmodel.dataArray = [bigModel getProductInfoArrayWithGoodArray:result.goods_attrs isTender:NO];
        BGMessageContentFrameModel *ProFmodel = [[BGMessageContentFrameModel alloc]init];
        ProFmodel.dataModel = Productmodel;
        //计算行高
        ProFmodel.cellHeigh = Productmodel.dataArray.count ? BGTitleHeigh + ((Productmodel.dataArray.count + 1) /2 ) * BGLabelHeigh + ((Productmodel.dataArray.count + 1) /2 +1) * BGMagListMaginY + BGMaginButtom : BGTitleHeigh;
        [dataArray addObject:ProFmodel];
        //质检报告
        BGQCreportDataModel *QcModel = [[BGQCreportDataModel alloc]init];
        QcModel.title = @"质检报告";
        NSMutableArray *QcDataArray = [NSMutableArray new];
        for (BGCheckReportModel *UrlModel in result.check_reports) {
            NSString *basicUrl = [BGBasicUrl getBasicUrl];
            GWLog(@"%@",UrlModel.url);
            NSString *url = [[BGBasicUrl getBasicUrl] stringByAppendingString:UrlModel.url];//拼接图片URL
            //        NSString *url = @"s";
            GWLog(@"%@",url);
            [QcDataArray addObject:url];
        }
        QcModel.imageDataArray = QcDataArray;
        BGMessageContentFrameModel *QcFmodel = [[BGMessageContentFrameModel alloc]init];
        QcFmodel.dataModel = QcModel;
        //    计算行高
        QcFmodel.cellHeigh = QcModel.imageDataArray.count ? BGTitleHeigh + ((QcModel.imageDataArray.count + 1) /2 ) * BGImgListHeigh + ((QcModel.imageDataArray.count + 1) /2 +1) * BGMagListMaginY :BGTitleHeigh;
        [dataArray addObject:QcFmodel];
//
//        for (BGMessageContentFrameModel *m in dataArray) {
//            GWLog(@"%@",[m.dataModel class]);
//        }
//        BGbidRecordListDataModel *bidListModel = [BGbidRecordListDataModel new];
//        bidListModel.title = @"标题";
//        bidListModel.datetext = @"时间";
//        bidListModel.contentText = @"内容";
//        BGMessageContentFrameModel *bidListFmodel = [[BGMessageContentFrameModel alloc]init];
//        //    bidListModel.dataModel = bidListModel;
//        bidListFmodel.dataModel = bidListModel;
//        bidListFmodel.cellHeigh = 80;
//        [dataArray addObject:bidListFmodel];
    
        
        return dataArray;
    
}

+ (NSArray *)getDataArrayWithBgTenderInfoResult:(BGGoodInfoResult *)result
{
    NSMutableArray *dataArray = [NSMutableArray array];
    BGGoodInfoFramDataModel *bigModel = [[BGGoodInfoFramDataModel alloc]init];
    BGMessageContentFrameModel *Fmodel = [[BGMessageContentFrameModel alloc]init];
    //顶部
//    BGAuctionTopViewDataModel *auctionModel = [BGAuctionTopViewDataModel new];
//    auctionModel.title = result.title;
//    BGGivePriceTopViewDataModel
    BGProductDetialDataModel *model = [[BGProductDetialDataModel alloc]init];
    model.title = result.title;
    model.secStr = [NSString stringWithFormat:@"浏览人气%@",result.view_times];
    model.supplyQuantity = result.origin_number;
    model.supplyQuantityStr = @"需求量";
    model.price = result.price;
    model.priceStr = @"采购单价(元)";
    model.browsePopularity = result.go_bid_num;
    model.browsePopularityStr = @"投标人数";
    model.doubleLineContentListArray = result.detialArrayTender;
    model.endTime = result.end_time;
    model.isDouble = YES;
    Fmodel.dataModel = model;
    //计算高度
    //    CGFloat contHeigh = BGTitleHeigh * 2;
    //    CGFloat listH = BGTitleHeigh * 3;
    //    CGFloat timeHeigh = BGTitleHeigh * 4;
    
    NSInteger temp = result.detialArrayTender.count;
    Fmodel.cellHeigh = BGTitleHeigh + BGTopMidViewHeigh + temp * BGMagListMaginY + temp * BGLabelHeigh + 5 + 2 * BGMaginTop + 2 * BGLabelHeigh + BGMaginButtom ;
    [dataArray addObject:Fmodel];
    
    
    
    //产品指标
    BGProductIndexDataModel *Productmodels = [[BGProductIndexDataModel alloc]init];
    Productmodels.title = @"基本信息";
    //循环设置数组
    Productmodels.dataArray = result.detialArrayTenderInfo;
    BGMessageContentFrameModel *ProFmodels = [[BGMessageContentFrameModel alloc]init];
    ProFmodels.dataModel = Productmodels;
    //计算行高
    ProFmodels.cellHeigh = Productmodels.dataArray.count ? BGTitleHeigh + ((Productmodels.dataArray.count + 1) /2 ) * BGLabelHeigh + ((Productmodels.dataArray.count + 1) /2 +1) * BGMagListMaginY + BGMaginButtom : BGTitleHeigh;
    [dataArray addObject:ProFmodels];
    
    
    
    //查看阶梯价格
//    BGMessageContentFrameModel *visitPriceFmodel = [[BGMessageContentFrameModel alloc]init];
//    BGViewladderpriceDataModel *visitPModel = [[BGViewladderpriceDataModel alloc]init];
//    //    visitPModel.title = @""
//    
//    visitPModel.dataArray = [BGStatePriceStrings arrayWithResultArray:result.step_price andUnit:result.unit];
//    visitPriceFmodel.dataModel = visitPModel;
//    visitPriceFmodel.cellHeigh = BGButtonCellHeigh;
//    [dataArray addObject:visitPriceFmodel];
    
    //产品指标
    BGProductIndexDataModel *Productmodel = [[BGProductIndexDataModel alloc]init];
    Productmodel.title = @"产品指标";
    //循环设置数组
    Productmodel.dataArray = [bigModel getProductInfoArrayWithGoodArray:result.goods_attrs isTender:YES];
    BGMessageContentFrameModel *ProFmodel = [[BGMessageContentFrameModel alloc]init];
    ProFmodel.dataModel = Productmodel;
    //计算行高
    ProFmodel.cellHeigh = Productmodel.dataArray.count ? BGTitleHeigh + ((Productmodel.dataArray.count + 1) /2 ) * BGLabelHeigh + ((Productmodel.dataArray.count + 1) /2 +1) * BGMagListMaginY + BGMaginButtom : BGTitleHeigh;
    [dataArray addObject:ProFmodel];
    //质检报告
    BGQCreportDataModel *QcModel = [[BGQCreportDataModel alloc]init];
    QcModel.title = @"质检报告";
    NSMutableArray *QcDataArray = [NSMutableArray new];
    for (BGCheckReportModel *UrlModel in result.check_reports) {
        NSString *basicUrl = [BGBasicUrl getBasicUrl];
        GWLog(@"%@",UrlModel.url);
        NSString *url = [[BGBasicUrl getBasicUrl] stringByAppendingString:UrlModel.url];//拼接图片URL
        //        NSString *url = @"s";
        GWLog(@"%@",url);
        [QcDataArray addObject:url];
    }
    QcModel.imageDataArray = QcDataArray;
    BGMessageContentFrameModel *QcFmodel = [[BGMessageContentFrameModel alloc]init];
    QcFmodel.dataModel = QcModel;
    //    计算行高
    QcFmodel.cellHeigh = QcModel.imageDataArray.count ? BGTitleHeigh + ((QcModel.imageDataArray.count + 1) /2 ) * BGImgListHeigh + ((QcModel.imageDataArray.count + 1) /2 +1) * BGMagListMaginY :BGTitleHeigh;
//    [dataArray addObject:QcFmodel];
    
    for (BGMessageContentFrameModel *m in dataArray) {
        GWLog(@"%@",[m.dataModel class]);
    }
//    BGbidRecordListDataModel *bidListModel = [BGbidRecordListDataModel new];
//    bidListModel.title = @"标题";
//    bidListModel.datetext = @"时间";
//    bidListModel.contentText = @"内容";
//    BGMessageContentFrameModel *bidListFmodel = [[BGMessageContentFrameModel alloc]init];
////    bidListModel.dataModel = bidListModel;
//    bidListFmodel.dataModel = bidListModel;
//    bidListFmodel.cellHeigh = 80;
//    [dataArray addObject:bidListFmodel];

    
    return dataArray;
}

- (NSArray *)getProductInfoArrayWithGoodArray:(NSArray *)array isTender:(BOOL)isTender
{
    NSMutableArray *productInfoArray = [NSMutableArray new];
//    NSArray *a = _goods_attrs;
//    int i = 0;
    NSMutableDictionary *tempDic = [[NSMutableDictionary alloc]initWithDictionary:@{@"name":@"",@"value":@""}];
    for (BGGoodAttrsModel *m in array) {
//        if (![m.attr_type  isEqual: @"3"] && ![m.attr_type  isEqual: @"5"]) {
//            NSString * str = [m.default_value firstObject];
//            [productInfoArray addObject:[self getDictWithTitle:m.name andValue:str]];
//        }
        NSString * str;
        if ([m.default_value  isKindOfClass:[NSArray class]]) {
//            str = [m.default_value firstObject];
            str = [m.default_value componentsJoinedByString:@","];
            [productInfoArray addObject:[self getDictWithTitle:m.name andValue:str]];
        }
        
//        NSString * str = [m.default_value firstObject];
        if (isTender) {
            
            if ([m.attrType isEqualToString:@"4"] && [m.name isEqualToString:tempDic[@"name"]]) {
                tempDic[@"value"] = [NSString stringWithFormat:@"%@%@",tempDic[@"value"],m.value];
            }else if ([m.attrType isEqualToString:@"4"] && ![m.name isEqualToString:tempDic[@"name"]] ){
                if (![tempDic[@"name"] isEqualToString:@""]) {
                    [productInfoArray addObject:[self getDictWithTitle:tempDic[@"name"] andValue:tempDic[@"value"]]];
                }
                tempDic[@"name"] = m.name;
                tempDic[@"value"] = m.value;
            }else if (![m.attrType isEqualToString:@"4"] && ![tempDic[@"name"] isEqualToString:@""]){
                [productInfoArray addObject:[self getDictWithTitle:tempDic[@"name"] andValue:tempDic[@"value"]]];
                str = m.value;
                [productInfoArray addObject:[self getDictWithTitle:m.name andValue:str]];
                tempDic[@"name"] = @"";
                tempDic[@"value"] = @"";
            }else{
                str = m.value;
                [productInfoArray addObject:[self getDictWithTitle:m.name andValue:str]];
                
            }
            
            
//            if (i%2 == 0) {
//                str = m.value;
//            }else{
////            str = m.value;
//            str = [NSString stringWithFormat:@"%@",m.value];
//            }
        }
//        [productInfoArray addObject:[self getDictWithTitle:m.name andValue:str]];
//        i++;
    }
    
    return productInfoArray;
}

- (NSDictionary *)getDictWithTitle:(NSString *)title andValue:(NSString *)value
{
    if (!title) {
        title = @"";
    }
    if (!value) {
        value = @"";
    }
    return @{
             @"title":title,
             @"value":value
             };
}
@end
